#include "pluginslist.h"
#include "myapp.h"
#include <QRadioButton>
#include "SelectPluginButton.h"
#include <QSettings>

//
pluginsList::pluginsList(QObject *parent, QSettings *Config_, addImagePluginStratum *addImageStratum )
	: QObject(parent),
	Config(Config_)
{
	// TODO
	setAddImageStratum(addImageStratum);
	connect(this, SIGNAL(setIcon(QIcon)), parent, SLOT(setUploadIcon(QIcon)));
	connect(this, SIGNAL(addPluginRow(QStringList)), parent, SLOT(addPluginRow(QStringList)));
	connect(this, SIGNAL(addPluginButton(SelectPluginButton*)), parent, SLOT(addPluginButton(SelectPluginButton*)));
	connect(this, SIGNAL(addSettingsTab(QWidget*, QIcon, QString)), parent, SLOT(addSettingsTab(QWidget*, QIcon, QString)));
}
//



bool pluginsList::loadPlugins()
{
	bool ret = false;
	currentPlugin = NULL;
	QStringList dirs = MyApp::pluginsDirs();
	foreach(QString dir, dirs) {
	QDir pluginsDir(dir);
	pluginsDir.cd("plugins");
	foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
		qDebug() << "Found plugin:" << fileName;
		QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
		QObject *plugin = loader.instance();
		if (plugin) {
			if ( uploadPluginClass *uploadPlugin = qobject_cast<uploadPluginClass *>(plugin) )
			{
				if (loadUploadPlugin(uploadPlugin) ) {
					ret = true;
					pluginFileNames += fileName;
				}
			}
			else if ( addImagePluginClass *addImagePlugin = qobject_cast<addImagePluginClass *>(plugin) )
			{
				if (loadAddImagePlugin(addImagePlugin) ) {
					//ret = true;
					pluginFileNames += fileName;
				}
			}
			else
			{
				qWarning() << "plugin inherit some my interface, but, for strange, I don't know this interface";
				loader.unload();
			}
		} else
		{ qWarning() << "Plugin"<< fileName << "not loaded:" << loader.errorString(); }
	}
	}
	qDebug() << "Loaded plugins:\n" << pluginFileNames;
	return ret;
}



bool pluginsList::loadAddImagePlugin(addImagePluginClass *plugin)
{
	emit addPluginRow(QStringList() << plugin->About.Name << plugin->About.Description << plugin->About.Author );
	QString plLabel = plugin->About.Name;
	plugin->setStratum(this->getAddImageStratum());
	addImagePlugins.append(plugin);

	QWidget *tab = new QWidget();
	QGridLayout *lout = plugin->getSettingsTab(tab);
	if ( lout ) {
		tab->setLayout(lout);
//		ui.toolBox->addItem(tab, icon, plLabel);
		emit addSettingsTab(tab, plugin->About.icon, plLabel);
	} else {
		delete(tab);
	}
	return true;
}


bool pluginsList::loadUploadPlugin(uploadPluginClass *plugin)
{
	QString plLabel = plugin->About.Name;
	emit addPluginRow( QStringList() << plugin->About.Name << plugin->About.Description << plugin->About.Author );

	QIcon icon = plugin->About.icon;
	SelectPluginButton *pluginbutton = new SelectPluginButton(plLabel, plugin);
	pluginbutton->setIcon(icon);
	connect(pluginbutton, SIGNAL(toggled(bool, uploadPluginClass*)), this, SLOT(reSelectPlugin(bool, uploadPluginClass*)));
	emit addPluginButton(pluginbutton);

	QWidget *tab = new QWidget();
	QLayout *lout = plugin->getSettingsTab(tab);
	if ( lout ) 
	{
		tab->setLayout(lout);
//		ui.toolBox->addItem(tab, icon, plLabel);
		emit addSettingsTab(tab, icon, plLabel);
	} else {
		delete(tab);
	}
	if ( plugin->About.Name == Config->value("plugins/default") or ! Config->contains("plugins/default") )
	{
		pluginbutton->setChecked(true);
		reSelectPlugin(true, plugin);
		
	}
	uploadPlugins.append(plugin);
	return true;
}


void pluginsList::reSelectPlugin(const bool checked, uploadPluginClass *plugin)
{
	if (checked)
	{
		this->currentPlugin = plugin;
		Config->setValue("plugins/default", plugin->About.Name);
		emit setIcon(plugin->About.icon);
	}
}

void pluginsList::retranslatePlugins()
{
	foreach ( uploadPluginClass *plugin, uploadPlugins )
	{ plugin->retranslate(); }
	foreach ( addImagePluginClass *plugin, addImagePlugins )
	{ plugin->retranslate(); }
}


